package com.git_taoxian.test2;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
//        定义一个ArrayList集合
        ArrayList<Integer> list = new ArrayList<>();
//        获取当前类的对象
        Class<? extends ArrayList> clz = list.getClass();
        try {
//        获取当前add方法的对象
            Method method = clz.getMethod("add", Object.class);
//        执行add方法
         Object rs =  method.invoke(list, "hello");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
//        打印输出该集合
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}
